package web;

import model.User;

import org.apache.struts2.interceptor.PrincipalAware;
import org.apache.struts2.interceptor.PrincipalProxy;

import service.UserService;

import com.opensymphony.xwork2.ActionSupport;

import exceptions.UserNotFoundException;

/**
 * BLMS main action.
 * @author Andre Melo
 * @author Marcus Leite 
 * @author Sergio Danilo
 *
 */
public class BLMSAction extends ActionSupport implements PrincipalAware {

	private static final long serialVersionUID = -5468730062182879677L;

	/*
	 * The instance of UserService.
	 */
	protected UserService userService;

	/*
	 * The principal proxy.
	 */
	private PrincipalProxy principalProxy;

	@Override
	public void setPrincipalProxy(PrincipalProxy arg0) {
		this.principalProxy = arg0;
	}
	
	/**
	 * Get the user in current session.
	 * @return The user in current session.
	 */
	public User getSessionUser() {
		String login = principalProxy.getRemoteUser();
		try {
			User userSession = userService.findByEmail(login);
			return userSession;
		} catch (UserNotFoundException e) {}
		return null;
	}

	/**
	 * Get the instance of UserService.
	 * @return The instance of UserService.
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * Set the instance of UserService.
	 * @param userService The instance of UserService.
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
